<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>oop的练习</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }

        #app{
            width: 1090px;
            margin: 50px auto;
        }
        p{
            margin: 50px;
            font-size: 25px;
        }
        input{
            width: 300px;
            height: 30px;
            margin-left: 150px;
        }
        button{
            width: 100px;
            height: 35px;
            line-height: 30px;
        }
        #ulList{
            width: 800px;
            min-height: 300px;
            background: antiquewhite;
            list-style: none;
            margin: 50px auto;
        }

        .myLi{
            width: 100%;
            height: 30px;
            line-height: 30px;
            font-weight: bold;
            background: black;
            color: #ffffff;
            font-size: 18px;
            text-indent: 10px;
            margin-top: 10px;
        }
    </style>
</head>
<body>
<div id="app">

    <p>实现一个需求,在输入框中输入内容的时候,将内容添加到ul中 提示 li</p>

    <input type="text" id="name">
    <button id="add">添加</button>

    <ul id="ulList">

    </ul>
</div>

<script>
    //使用oop的写法完成功能
    "use strict";
    const DATA = Symbol();
    class AddEl{
        constructor(nameInput,addButton,ulList) {
            this[DATA] = {nameInput,addButton,ulList};
        }
        get nameInput(){
            return this[DATA].nameInput;
        }
        set nameInput(value){
            this[DATA].nameInput = value;
        }

        get addButton(){
            return this[DATA].addButton;
        }
        set addButton(value){
            this[DATA].addButton = value;
        }
        get ulList(){
            return this[DATA].ulList;
        }
        set ulList(value){
            this[DATA].ulList = value;
        }

        //创建元素
        createEle(targetEl,nameValue){
            //append追加元素，到尾部
            var fragment = document.createDocumentFragment();
            let li_ele = document.createElement(targetEl);
            li_ele.className = 'myLi';
            li_ele.innerText = nameValue;
            fragment.appendChild(li_ele)
            return fragment;
        }

        //键盘事件
        myKeyUp(){
            this[DATA].nameInput.onkeyup = ((e) => {
                if(e.keyCode == 13){
                    this.doAppend();
                }
            });
        }
        //鼠标点击事件
        myMouseClick(){
            this[DATA].addButton.onclick = (() => {
                this.doAppend();
            })
        }
        //追加元素
        doAppend(){
            let nameValue = this[DATA].nameInput.value;
            if(nameValue.length == 0){
                alert("不能为空...");
                return false;
            }
            //创建标签
            let fragment = this.createEle('li',nameValue);
            this[DATA].ulList.append(fragment);
            this[DATA].nameInput.value = "";
        }
    }

    //进行初始化操作
    let addel = new AddEl($("name"),$("add"),$("ulList"));

    //将一切封装好之后,只需要调用相关的方法即可
    //调用点击事件
    addel.myMouseClick();
    addel.myKeyUp();
    function $(el){
        return document.getElementById(el);
    }


</script>
</body>
</html>